/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef CONNECTION_SERIAL_H
#define CONNECTION_SERIAL_H

#include <QHostAddress>
#include <QString>
#include <QTcpSocket>
#include <QTime>
#include <QTimer>
#include <QStack>
#include <QPoint>

#include <QtCore/QtGlobal>
#include <QtSerialPort/QSerialPort>

#include "connection.h"

class SettingsDialog;
class QSerialPort;

class CxConnection_serial : public CxConnection//: public QTcpSocket
{
    Q_OBJECT

public:
     CxConnection_serial(Dialog *pdialog);
     CxConnection_serial(Dialog *pdialog,int icos);
     CxConnection_serial(Dialog *pdialog,QTcpSocket *parent);
     ~CxConnection_serial();

    SettingsDialog *m_settings;
    QSerialPort *m_serial;
    int m_iserialgetdata;
    int m_iserialclear;
public slots:
    void startTransfer();
    void stopTransfer();
    void openSerialPort();
    void closeSerialPort();
    //void about();
    void writeData(const QByteArray &data);
    void readData_tcpip();
    void readData_serial();
    void ProcessDataBuffClear_serial();
    void ProcessDataBuff_serial();
    void handleError(QSerialPort::SerialPortError error);

public:
    virtual QString getio_local_address_string();
    virtual QString getio_peer_address_string();
    virtual int iolocalport();
    virtual int iopeerport();
    virtual qint64 iobytesAvailable();
    virtual bool iovalid();
    virtual void ioclose();
    virtual qint64 iowrite(const QByteArray &data);
    virtual QByteArray ioread(qint64 maxlen);
    virtual void ioabort();
    virtual QByteArray ioreadall();

    void senddir(const QString &fileName);
    void packagedata(const QByteArray &data);
    virtual bool sendCommand(const QString &message,const QString &stid="00000000");
    bool sendfile(const QString &fileName);
    bool sendfilex(const QString &fileName);
    void transforstart();
    int ParserArray(QByteArray &strarray,int& ibeginsign,int& iendsign);
    void parserpackagestring();
    bool senddesktopimage();
    void sendimage(QImage &aimage);
    void sendvalue(QString &astrname,QString &strvalue);
    void sendstring(QString &astring);
    void visionaireq(QByteArray &strarray);
    void reset();
private:
    QByteArray m_readData;

    QStack<QByteArray> m_readBuf;
    QStack<int> m_buftype;
    QStack<QPoint> m_beginend;

    int m_iprocessover;
    int m_icurdealnum;
    int m_iclearnum;
    int m_ifindhead;
    int m_ifindtail;

    int m_iarraysize;


    QTimer m_retrytimer;
    QMap<int,QByteArray> m_filelist;
    int m_isendok;

    int m_itype;

    int m_igetpackage;
    //int m_isretry;
    QByteArray m_retrydata;

    QTcpSocket *m_tcpClient;
    int m_iconnected;
    bool m_bgetdata;
    int m_icontinuetimes;
    QBasicTimer m_eventtimer;
    QBasicTimer m_processeventtimer;
    QByteArray m_senddata;

    int m_igetping;
    int m_igetpong;

    int m_ireadytotransfor;

    int m_ihead;

    int m_ifinishsendfile;

    void timerEvent(QTimerEvent *event);

    void sendData();

    virtual QByteArray EreadAll();
private slots:
    void retryrun();

public slots:
    virtual void overconnect();
public:
    virtual void processData(){CxConnection::processData();}
    virtual void processRead();
};

#endif
